﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Client.Components.Icons;

namespace Client.Components.ToolbarControls
{
    // V 2.0.0 - 2025-09-16 15:02:55
    // 1. 添加依赖属性 Icon
    // 2. 弃用 AddButton, EditButton 等继承出来的类
    // 3. ToolbarButtonBase 重命名为 ToolbarButton
    //
    // V 1.0.1 - 2021-07-14 17:43:20
    // 优化 对于 ImageUri 的赋值 使用 Uri2ImageSourceConverter 进行转换
    // 
    // V 1.0.0 - 2021-07-14 17:31:23
    // 首次创建

    /// <summary>
    /// ToolbarButtonBase
    /// </summary>
    public partial class ToolbarButton : Button
    {
        /// <summary>
        /// ToolbarButtonBase
        /// </summary>
        public ToolbarButton()
        {
            InitializeComponent();
        }

        #region [DP] ImageWidth

        /// <summary>
        /// ImageWidth
        /// </summary>
        public static readonly DependencyProperty ImageWidthProperty = DependencyProperty.Register
        (
            name: "ImageWidth",
            propertyType: typeof(double),
            ownerType: typeof(ToolbarButton),
            validateValueCallback: null, // new ValidateValueCallback((toValidate) => { return true; }),
            typeMetadata: new PropertyMetadata
            (
                defaultValue: 20d,
                propertyChangedCallback: null,
                coerceValueCallback: null
            )
        );

        /// <summary>
        /// ImageWidth
        /// </summary>
        public double ImageWidth
        {
            get { return (double)GetValue(ImageWidthProperty); }
            set { SetValue(ImageWidthProperty, value); }
        }

        #endregion

        #region [DP] ImageHeight

        /// <summary>
        /// ImageHeight
        /// </summary>
        public static readonly DependencyProperty ImageHeightProperty = DependencyProperty.Register
        (
            name: "ImageHeight",
            propertyType: typeof(double),
            ownerType: typeof(ToolbarButton),
            validateValueCallback: null, // new ValidateValueCallback((toValidate) => { return true; }),
            typeMetadata: new PropertyMetadata
            (
                defaultValue: 20d,
                propertyChangedCallback: null,
                coerceValueCallback: null
            )
        );

        /// <summary>
        /// ImageHeight
        /// </summary>
        public double ImageHeight
        {
            get { return (double)GetValue(ImageHeightProperty); }
            set { SetValue(ImageHeightProperty, value); }
        }

        #endregion

        #region [DP] ImageUri

        /// <summary>
        /// ImageUri
        /// </summary>
        public static readonly DependencyProperty ImageUriProperty = DependencyProperty.Register
        (
            name: "ImageUri",
            propertyType: typeof(Uri),
            ownerType: typeof(ToolbarButton),
            validateValueCallback: null,
            typeMetadata: new PropertyMetadata
            (
                defaultValue: null,
                propertyChangedCallback: null, // onImageUri_PropertyChangedCallback,
                coerceValueCallback: null
            )
        );

        /// <summary>
        /// ImageUri
        /// </summary>
        public Uri ImageUri
        {
            get { return (Uri)GetValue(ImageUriProperty); }
            set { SetValue(ImageUriProperty, value); }
        }

        private static void onImageUri_PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //// 优化 对于 ImageUri 的赋值 使用 Uri2ImageSourceConverter 进行转换
            ///
            //if (d is ToolbarButtonBase target)
            //{
            //    if (string.IsNullOrWhiteSpace(e.NewValue.ToString()) == false)
            //    {
            //        BitmapImage image = new BitmapImage();
            //        image.BeginInit();
            //        image.UriSource = e.NewValue as Uri;
            //        image.EndInit();
            //        target.image.Source = image;
            //    }
            //}
        }

        #endregion

        #region [DP] Title

        /// <summary>
        /// Title
        /// </summary>
        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register
        (
            name: "Title",
            propertyType: typeof(string),
            ownerType: typeof(ToolbarButton),
            validateValueCallback: null,
            typeMetadata: new PropertyMetadata
            (
                defaultValue: null,
                propertyChangedCallback: null,
                coerceValueCallback: null
            )
        );

        /// <summary>
        /// Title
        /// </summary>
        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        #endregion

        #region IconEnum

        /// <summary>
        /// IconProperty
        /// </summary>
        public static readonly DependencyProperty IconProperty = DependencyProperty.Register
        (
            name: "Icon",
            propertyType: typeof(IconEnum),
            ownerType: typeof(ToolbarButton),
            validateValueCallback: null, // new ValidateValueCallback((toValidate) => { return true; }),
            typeMetadata: new PropertyMetadata
            (
                defaultValue: IconEnum.NONE,
                propertyChangedCallback: onIcon_PropertyChangedCallback,
                coerceValueCallback: null
            )
        );

        /// <summary>
        /// 图标
        /// </summary>
        public IconEnum Icon
        {
            get { return (IconEnum)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }

        /// <summary>
        /// 将 IconEnum 转为 ImageUri
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        public static void onIcon_PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is ToolbarButton target)
            {
                IconEnum icon = (IconEnum)Enum.Parse(typeof(IconEnum), e.NewValue.ToString());
                string uriStr = IconEnumUtils.GetIconResourcePath(icon);
                target.ImageUri = new Uri(uriStr);
                
                if (string.IsNullOrWhiteSpace(target.Title))
                {
                    // 如果用户没有定义 Title, 将默认 Title 填上
                    target.Title = IconEnumUtils.GetDefaultValue(icon);
                }
            }
        }


        #endregion
    }

    #region 增删改查

    /// <summary>
    /// AddButton
    /// </summary>
    [Obsolete]
    public class AddButton : ToolbarButton
    {
        /// <summary>
        /// AddButton
        /// </summary>
        public AddButton()
        {
            base.Title = "添加";

            IconEnum icon = IconEnum.Add;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);            
            base.ImageUri = new Uri(uriStr);
        }
    }

    /// <summary>
    /// EditButton
    /// </summary>
    [Obsolete]
    public class EditButton : ToolbarButton
    {
        /// <summary>
        /// EditButton
        /// </summary>
        public EditButton()
        {
            base.Title = "编辑";

            IconEnum icon = IconEnum.Edit;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    /// <summary>
    /// DeleteButton
    /// </summary>
    [Obsolete]
    public class DeleteButton : ToolbarButton
    {
        /// <summary>
        /// DeleteButton
        /// </summary>
        public DeleteButton()
        {
            base.Title = "删除";

            IconEnum icon = IconEnum.Delete;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    /// <summary>
    /// SaveButton
    /// </summary>
    [Obsolete]
    public class SaveButton : ToolbarButton
    {
        /// <summary>
        /// SaveButton
        /// </summary>
        public SaveButton()
        {
            base.Title = "保存";

            IconEnum icon = IconEnum.Save;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    /// <summary>
    /// MinusButton
    /// </summary>
    [Obsolete]
    public class MinusButton : ToolbarButton
    {
        /// <summary>
        /// MinusButton
        /// </summary>
        public MinusButton()
        {
            base.Title = "减去";

            IconEnum icon = IconEnum.Minus;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    #endregion

    #region 全选反选

    /// <summary>
    /// CheckAllButton
    /// </summary>
    [Obsolete]
    public class CheckAllButton : ToolbarButton
    {
        /// <summary>
        /// CheckAllButton
        /// </summary>
        public CheckAllButton()
        {
            base.Title = "全选";

            IconEnum icon = IconEnum.CheckAll;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    /// <summary>
    /// ReverseCheckButton
    /// </summary>
    [Obsolete]
    public class ReverseCheckButton : ToolbarButton
    {
        /// <summary>
        /// ReverseCheckButton
        /// </summary>
        public ReverseCheckButton()
        {
            base.Title = "反选";

            IconEnum icon = IconEnum.ReverseCheck;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    #endregion

    #region 导入导出

    /// <summary>
    /// FileImportButton
    /// </summary>
    [Obsolete]
    public class FileImportButton : ToolbarButton
    {
        /// <summary>
        /// FileImportButton
        /// </summary>
        public FileImportButton()
        {
            base.Title = "导入";

            IconEnum icon = IconEnum.FileImport;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    /// <summary>
    /// FileExportButton
    /// </summary>
    [Obsolete]
    public class FileExportButton : ToolbarButton
    {
        /// <summary>
        /// FileExportButton
        /// </summary>
        public FileExportButton()
        {
            base.Title = "导出";

            IconEnum icon = IconEnum.FileExport;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    /// <summary>
    /// FileReloadButton
    /// </summary>
    [Obsolete]
    public class FileReloadButton : ToolbarButton
    {
        /// <summary>
        /// FileReloadButton
        /// </summary>
        public FileReloadButton()
        {
            base.Title = "重新加载";

            IconEnum icon = IconEnum.Reset;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    /// <summary>
    /// ExcelExportButton
    /// </summary>
    [Obsolete]
    public class ExcelExportButton : ToolbarButton
    {
        /// <summary>
        /// ExcelExportButton
        /// </summary>
        public ExcelExportButton()
        {
            base.Title = "导出";

            IconEnum icon = IconEnum.ExcelExport;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    /// <summary>
    /// PrintButton
    /// </summary>
    [Obsolete]
    public class PrintButton : ToolbarButton
    {
        /// <summary>
        /// PrintButton
        /// </summary>
        public PrintButton()
        {
            base.Title = "打印";

            IconEnum icon = IconEnum.Print;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    #endregion

    #region 多媒体播放

    /// <summary>
    /// PlayButton
    /// </summary>
    [Obsolete]
    public class PlayButton : ToolbarButton
    {
        /// <summary>
        /// PlayButton
        /// </summary>
        public PlayButton()
        {
            base.Title = "播放";

            IconEnum icon = IconEnum.Play;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    /// <summary>
    /// PauseButton
    /// </summary>
    [Obsolete]
    public class PauseButton : ToolbarButton
    {
        /// <summary>
        /// PauseButton
        /// </summary>
        public PauseButton()
        {
            base.Title = "暂停";

            IconEnum icon = IconEnum.Pause;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    /// <summary>
    /// StopButton
    /// </summary>
    [Obsolete]
    public class StopButton : ToolbarButton
    {
        /// <summary>
        /// StopButton
        /// </summary>
        public StopButton()
        {
            base.Title = "停止";

            IconEnum icon = IconEnum.Stop;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    #endregion

    #region 旗帜

    /// <summary>
    /// RedFlagButton
    /// </summary>
    [Obsolete]
    public class RedFlagButton : ToolbarButton
    {
        /// <summary>
        /// RedFlagButton
        /// </summary>
        public RedFlagButton()
        {
            base.Title = "红旗";

            IconEnum icon = IconEnum.FlagRed;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    /// <summary>
    /// YellowFlagButton
    /// </summary>
    [Obsolete]
    public class YellowFlagButton : ToolbarButton
    {
        /// <summary>
        /// YellowFlagButton
        /// </summary>
        public YellowFlagButton()
        {
            base.Title = "黄旗";

            IconEnum icon = IconEnum.FlagYellow;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    /// <summary>
    /// GreenFlagButton
    /// </summary>
    [Obsolete]
    public class GreenFlagButton : ToolbarButton
    {
        /// <summary>
        /// GreenFlagButton
        /// </summary>
        public GreenFlagButton()
        {
            base.Title = "绿旗";

            IconEnum icon = IconEnum.FlagGreen;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    #endregion

    /// <summary>
    /// ReloadButton
    /// </summary>
    [Obsolete]
    public class ReloadButton : ToolbarButton
    {
        /// <summary>
        /// ReloadButton
        /// </summary>
        public ReloadButton()
        {
            base.Title = "刷新";

            IconEnum icon = IconEnum.Reset;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    /// <summary>
    /// SearchButton
    /// </summary>
    [Obsolete]
    public class SearchButton : ToolbarButton
    {
        /// <summary>
        /// SearchButton
        /// </summary>
        public SearchButton()
        {
            base.Title = "搜索";

            IconEnum icon = IconEnum.Search;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    /// <summary>
    /// HistoryButton
    /// </summary>
    [Obsolete]
    public class HistoryButton : ToolbarButton
    {
        /// <summary>
        /// HistoryButton
        /// </summary>
        public HistoryButton()
        {
            base.Title = "历史记录";

            IconEnum icon = IconEnum.History;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }

    /// <summary>
    /// QRCodeButton
    /// </summary>
    [Obsolete]
    public class QRCodeButton : ToolbarButton
    {
        /// <summary>
        /// QRCodeButton
        /// </summary>
        public QRCodeButton()
        {
            base.Title = "二维码";

            IconEnum icon = IconEnum.QRCode;
            string uriStr = Client.Components.IconEnumUtils.GetIconResourcePath(icon);
            base.ImageUri = new Uri(uriStr);
        }
    }
}
